package com.xsq.newmall01.Controller;

import com.xsq.newmall01.Dao.MerchantUser;
import com.xsq.newmall01.Service.MerchantUserService.MUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/merchant")
public class MerchantUserController {

    @Autowired
    @Qualifier("merchantuserservice")
    private MUserService merchantMUserService;
    /**
     * 商家登录
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody MerchantUser merchantUser) {
        Map<String, Object> result = new HashMap<>();
        MerchantUser loginUser = merchantMUserService.checkMerchantUserExists(merchantUser);
        if (loginUser != null) {
            // 新增状态检查：如果商家被禁用（status=0），拒绝登录
            if (loginUser.getStatus() == 0) {
                result.put("success", false);
                result.put("message", "账户被禁用，请联系管理员");
            } else {
                result.put("success", true);
                result.put("data", loginUser);
                result.put("message", "登录成功");
            }
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    /**
     * 更新商家信息
     */
    @PutMapping("/update")
    public Map<String, Object> updateMerchantUser(@RequestBody MerchantUser merchantUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            merchantMUserService.updateMerchantUser(merchantUser);
            result.put("success", true);
            result.put("message", "信息更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "信息更新失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/{id}")
    public Map<String, Object> getMerchantById(@PathVariable int id) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 这里需要在MUserService中添加getMerchantById方法
            MerchantUser merchant = merchantMUserService.getMerchantById(id);
            if (merchant != null) {
                result.put("success", true);
                result.put("data", merchant);
            } else {
                result.put("success", false);
                result.put("message", "商家不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @GetMapping("/info")
    public Map<String, Object> getMerchantPublicInfo(@RequestParam int id) {
        Map<String, Object> result = new HashMap<>();
        try {
            MerchantUser merchant = merchantMUserService.getMerchantById(id);
            if (merchant == null) {
                result.put("success", false);
                result.put("message", "商家不存在");
                return result;
            }

            // 创建一个只包含公开信息的Map
            Map<String, Object> publicInfo = new HashMap<>();
            publicInfo.put("shopname", merchant.getShopname());
            publicInfo.put("address", merchant.getAddress());
            publicInfo.put("shop_desc", merchant.getShop_desc());
            publicInfo.put("phone", merchant.getPhone());
            publicInfo.put("contact_person", merchant.getContact_person());

            result.put("success", true);
            result.put("data", publicInfo);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取商家信息失败：" + e.getMessage());
        }
        return result;
    }

}